package com.inwk.valoepi.pages;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

import com.inwk.valoepi.managers.PageManager;
import com.inwk.valoepi.model.User;

public class LoginPage extends Page {
	
	//private String loginPageXPathLocator = "//div[@class='customPortalContainer']//div[@class='custom-log-form']";
	private CharSequence loginPageURLPart = "LoginPage.aspx";
	
	@FindBy(xpath = "//div[@class='loginUserName-Portal']/input")
	private WebElement usernameField;
	
	@FindBy(xpath = "//div[@class='loginPassword-Portal']/input")
	private WebElement passwordField;
	
	@FindBy(xpath = "//div[@class='loginButton-Portal']/input")
	private WebElement submitButton;
	public LoginPage(PageManager pm) {
		super(pm);
	}
	
	
	private void performLogin(User user) {
		usernameField.clear();
		usernameField.sendKeys(user.getLogin());
		passwordField.clear();
		passwordField.sendKeys(user.getPassword());
		submitButton.click();
	}
	
	public MainPage loginAs(User user) {
		performLogin(user);
		/*
		if(pm.aboutPage.isCurrentPage()) {
			return pm.aboutPage.goNext();
		} else {
			return pm.mainPage;
		}*/
		return pm.mainPage;
	}

	@Override
	public boolean isCurrentPage() {
		if(webDriver.getCurrentUrl().contains(loginPageURLPart)) {
			return true;
		} else {
			return false;
		}
	}
}
